

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>7.10. Setting up logging — MDAnalysis.core.log &mdash; MDAnalysis v0.7.5-devel documentation</title>
    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.7.5-devel',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within MDAnalysis v0.7.5-devel documentation"
          href="../../_static/opensearch.xml"/>
    <link rel="shortcut icon" href="../../_static/mdanalysis-logo.ico"/>
    <link rel="top" title="MDAnalysis v0.7.5-devel documentation" href="../../index.html" />
    <link rel="up" title="7. Core modules" href="../core_modules.html" />
    <link rel="next" title="8. KDTree module" href="../KDTree_modules.html" />
    <link rel="prev" title="7.9. Helper functions — MDAnalysis.core.util" href="util.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../KDTree_modules.html" title="8. KDTree module"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="util.html" title="7.9. Helper functions — MDAnalysis.core.util"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">MDAnalysis v0.7.5-devel documentation</a> &raquo;</li>
          <li><a href="../core_modules.html" accesskey="U">7. Core modules</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/mdanalysis-logo-200x150.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">7.10. Setting up logging &#8212; <tt class="docutils literal"><span class="pre">MDAnalysis.core.log</span></tt></a><ul>
<li><a class="reference internal" href="#convenience-functions">7.10.1. Convenience functions</a></li>
<li><a class="reference internal" href="#other-functions-and-classes-for-logging-purposes">7.10.2. Other functions and classes for logging purposes</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="util.html"
                        title="previous chapter">7.9. Helper functions &#8212; <tt class="docutils literal"><span class="pre">MDAnalysis.core.util</span></tt></a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../KDTree_modules.html"
                        title="next chapter">8. KDTree module</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/documentation_pages/core/log.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <span class="target" id="module-MDAnalysis.core.log"></span><div class="section" id="setting-up-logging-mdanalysis-core-log">
<h1>7.10. Setting up logging &#8212; <a class="reference internal" href="#module-MDAnalysis.core.log" title="MDAnalysis.core.log"><tt class="xref py py-mod docutils literal"><span class="pre">MDAnalysis.core.log</span></tt></a><a class="headerlink" href="#setting-up-logging-mdanalysis-core-log" title="Permalink to this headline">¶</a></h1>
<p>Configure logging for MDAnalysis. Import this module if logging is
desired in application code.</p>
<p>Logging to a file and the console is set up by default as described
under <a class="reference external" href="http://docs.python.org/library/logging.html?#logging-to-multiple-destinations">logging to multiple destinations</a>.</p>
<p>The top level logger of the library is named <em>MDAnalysis</em> by
convention; a simple logger that writes to the console and logfile can
be created with the <a class="reference internal" href="#MDAnalysis.core.log.create" title="MDAnalysis.core.log.create"><tt class="xref py py-func docutils literal"><span class="pre">create()</span></tt></a> function. This only has to be done
<em>once</em>. For convenience, the default MDAnalysis logger can be created
with <tt class="xref py py-func docutils literal"><span class="pre">MDAnalysis.start_logger()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">MDAnalysis</span>
<span class="n">MDAnalysis</span><span class="o">.</span><span class="n">start_logger</span><span class="p">()</span>
</pre></div>
</div>
<p>Once this has been done, MDAnalysis will write messages to the logfile
(named <cite>MDAnalysis.log</cite> by default but this can be changed with the
optional argument to <tt class="xref py py-func docutils literal"><span class="pre">start_logger()</span></tt>).</p>
<p>Any code can log to the MDAnalysis logger by using</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;MDAnalysis.MODULENAME&#39;</span><span class="p">)</span>

<span class="c"># use the logger, for example at info level:</span>
<span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Starting task ...&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>The important point is that the name of the logger begins with
&#8220;MDAnalysis.&#8221;.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The <a class="reference external" href="http://docs.python.org/library/logging.html#module-logging" title="(in Python v2.7)"><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> module in the standard library contains
in depth documentation about using logging.</p>
</div>
<div class="section" id="convenience-functions">
<h2>7.10.1. Convenience functions<a class="headerlink" href="#convenience-functions" title="Permalink to this headline">¶</a></h2>
<p>Two convenience functions at the top level make it easy to start and
stop the default <em>MDAnalysis</em> logger.</p>
<dl class="function">
<dt id="MDAnalysis.start_logging">
<tt class="descclassname">MDAnalysis.</tt><tt class="descname">start_logging</tt><big>(</big><em>logfile='MDAnalysis.log'</em><big>)</big><a class="headerlink" href="#MDAnalysis.start_logging" title="Permalink to this definition">¶</a></dt>
<dd><p>Start logging of messages to file and console.</p>
<p>The default logfile is named <cite>MDAnalysis.log</cite> and messages are
logged with the tag <em>MDAnalysis</em>.</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.stop_logging">
<tt class="descclassname">MDAnalysis.</tt><tt class="descname">stop_logging</tt><big>(</big><big>)</big><a class="headerlink" href="#MDAnalysis.stop_logging" title="Permalink to this definition">¶</a></dt>
<dd><p>Stop logging to logfile and console.</p>
</dd></dl>

</div>
<div class="section" id="other-functions-and-classes-for-logging-purposes">
<h2>7.10.2. Other functions and classes for logging purposes<a class="headerlink" href="#other-functions-and-classes-for-logging-purposes" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="MDAnalysis.core.log.NullHandler">
<em class="property">class </em><tt class="descclassname">MDAnalysis.core.log.</tt><tt class="descname">NullHandler</tt><big>(</big><em>level=0</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.NullHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Silent Handler.</p>
<p>Useful as a default:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">h</span> <span class="o">=</span> <span class="n">NullHandler</span><span class="p">()</span>
<span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&quot;MDAnalysis&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
<span class="k">del</span> <span class="n">h</span>
</pre></div>
</div>
<p>Initializes the instance - basically setting the formatter to None
and the filter list to empty.</p>
</dd></dl>

<dl class="class">
<dt id="MDAnalysis.core.log.ProgressMeter">
<em class="property">class </em><tt class="descclassname">MDAnalysis.core.log.</tt><tt class="descname">ProgressMeter</tt><big>(</big><em>numsteps</em>, <em>format=None</em>, <em>interval=10</em>, <em>offset=0</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.ProgressMeter" title="Permalink to this definition">¶</a></dt>
<dd><p>Simple progress meter</p>
<blockquote>
<div><p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">u</span> <span class="o">=</span> <span class="n">Universe</span><span class="p">(</span><span class="n">PSF</span><span class="p">,</span> <span class="n">DCD</span><span class="p">)</span>
<span class="n">pm</span> <span class="o">=</span> <span class="n">ProgressMeter</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">trajectory</span><span class="o">.</span><span class="n">numframes</span><span class="p">,</span> <span class="n">interval</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
<span class="k">for</span> <span class="n">ts</span> <span class="ow">in</span> <span class="n">u</span><span class="o">.</span><span class="n">trajectory</span><span class="p">:</span>
    <span class="n">pm</span><span class="o">.</span><span class="n">echo</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">frame</span><span class="p">)</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>Will produce for a trajectory with 10000 frames</p>
<blockquote>
<div>Step   100/10000 [  1.0%]
Step   200/10000 [  2.0%]
...</div></blockquote>
<p>as generated by the default <em>format</em> string</p>
<div class="highlight-python"><pre>Step %(step)5d/%(numsteps)d [%(percentage)5.1f%%]</pre>
</div>
</div></blockquote>
<p>&#8220;</p>
<p>Set up the ProgressMeter</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Arguments :</th><td class="field-body"><dl class="first docutils">
<dt><em>numsteps</em></dt>
<dd><p class="first last">total number of steps</p>
</dd>
</dl>
</td>
</tr>
<tr class="field"><th class="field-name">Keywords :</th><td class="field-body"><dl class="first last docutils">
<dt><em>interval</em></dt>
<dd><p class="first last">only calculate progress every <em>interval</em> steps [10]</p>
</dd>
<dt><em>format</em></dt>
<dd><p class="first">a format string with Python variable interpolation. Allowed
values:</p>
<blockquote>
<div><ul class="simple">
<li>_step_: current step</li>
<li>_numsteps_: total number of steps as supplied in <em>numsteps</em></li>
<li>_percentage_: percentage of total</li>
</ul>
</div></blockquote>
<p>The last call to <tt class="xref py py-meth docutils literal"><span class="pre">ProgressMeter.print()</span></tt> will automatically
issue a newline <tt class="docutils literal"><span class="pre">\n</span></tt> if the last character is the carriage
return <tt class="docutils literal"><span class="pre">\r</span></tt>.</p>
<p class="last">If <em>format</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt> then the default is used.
[&#8220;Step %(step)5d/%(numsteps)d [%(percentage)5.1%%]r&#8221;]</p>
</dd>
<dt><em>offset</em></dt>
<dd><p class="first last">number to add to <em>step</em>; e.g. if <em>step</em> is 0-based then one would
set <em>offset</em> = 1 [0]</p>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="MDAnalysis.core.log.ProgressMeter.echo">
<tt class="descname">echo</tt><big>(</big><em>step</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.ProgressMeter.echo" title="Permalink to this definition">¶</a></dt>
<dd><p>Print the state to stderr, but only every <em>interval</em> steps.</p>
<ol class="arabic simple">
<li>calls <a class="reference internal" href="#MDAnalysis.core.log.ProgressMeter.update" title="MDAnalysis.core.log.ProgressMeter.update"><tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt></a></li>
<li>writes step and percentage to stderr with <a class="reference internal" href="#MDAnalysis.core.log.echo" title="MDAnalysis.core.log.echo"><tt class="xref py py-func docutils literal"><span class="pre">echo()</span></tt></a>,
using the format string (in <tt class="xref py py-attr docutils literal"><span class="pre">ProgressMeter.format</span></tt>)</li>
</ol>
<p>The last step is always shown, even if not on an <em>interval</em>, and a
carriage return is replaced with a new line for a cleaner display.</p>
</dd></dl>

<dl class="method">
<dt id="MDAnalysis.core.log.ProgressMeter.update">
<tt class="descname">update</tt><big>(</big><em>step</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.ProgressMeter.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Update the state of the ProgressMeter</p>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.log.clear_handlers">
<tt class="descclassname">MDAnalysis.core.log.</tt><tt class="descname">clear_handlers</tt><big>(</big><em>logger</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.clear_handlers" title="Permalink to this definition">¶</a></dt>
<dd><p>clean out handlers in the library top level logger</p>
<p>(only important for reload/debug cycles...)</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.log.create">
<tt class="descclassname">MDAnalysis.core.log.</tt><tt class="descname">create</tt><big>(</big><em>logger_name='MDAnalysis'</em>, <em>logfile='MDAnalysis.log'</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.create" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a top level logger.</p>
<ul class="simple">
<li>The file logger logs everything (including DEBUG).</li>
<li>The console logger only logs INFO and above.</li>
</ul>
<p>Logging to a file and the console as described under <a class="reference external" href="http://docs.python.org/library/logging.html?#logging-to-multiple-destinations">logging to
multiple destinations</a>.</p>
<p>The top level logger of MDAnalysis is named <em>MDAnalysis</em>.  Note
that we are configuring this logger with console output. If a root
logger also does this then we will get two output lines to the
console.</p>
</dd></dl>

<dl class="function">
<dt id="MDAnalysis.core.log.echo">
<tt class="descclassname">MDAnalysis.core.log.</tt><tt class="descname">echo</tt><big>(</big><em>s=''</em><big>)</big><a class="headerlink" href="#MDAnalysis.core.log.echo" title="Permalink to this definition">¶</a></dt>
<dd><p>Simple string output that immediately prints to the console.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../KDTree_modules.html" title="8. KDTree module"
             >next</a> |</li>
        <li class="right" >
          <a href="util.html" title="7.9. Helper functions — MDAnalysis.core.util"
             >previous</a> |</li>
        <li><a href="../../index.html">MDAnalysis v0.7.5-devel documentation</a> &raquo;</li>
          <li><a href="../core_modules.html" >7. Core modules</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2005-2011, Naveen Michaud-Agrawal, Elizabeth J. Denning, Joshua Adelman,
    Christian Beckstein (logo), David Caplan, Jan Domański, Philip Fowler,
    Joseph Goose, Benjamin Hall, Danny Parton, Tyler Reddy, Paul Rigor,
    and Oliver Beckstein.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>
  </body>
</html>